<?php

/*  
==================================================================
1. PERIODE GRIEVANCE
==================================================================
*/
function list_periode(){
	my_set_code_js('

		function confirmDelete(id){
			var t = confirm(\'Yakin akan menghapus data ?\');
			if(t){
				location.href=\'index.php?com=grievance&task=grievance_periode_delete&id=\'+id;
			}
			return false;
		}
		');	

	$header = array(
		'Periode'=>array('style'=>'text-align:left;border-bottom:2px solid;width:40%'),  
		' '=>array('style'=>'border-bottom:2px solid;width:60%'), 
	);
	
	$query = "SELECT * FROM grievance_periode";
	$result = my_query($query);
	$row=array();
	while(	$ey = my_fetch_array($result) ){
		$editproperty = array(
						'href'=>'index.php?com=grievance&task=grievance_periode_create&pid='.$ey['grievance_periode_id'],
						'rel'=>'facebox',
						'title'=>'Edit'
				);
		$edit_button = button_icon( 'b_edit.png' , $editproperty  );

		$detailproperty = array(
						'href'=>'index.php?com=grievance&task=list_grievance_kelompok&pid='.$ey['grievance_periode_id'],
						'title'=>'Info detail'
				);
		$detail_button = button_icon( 'b_props.png' , $detailproperty  );

		$deleteproperty = array(
						'href'=>'javascript:; ',
						'onclick'=>'javascript:confirmDelete('.$ey['grievance_periode_id'].');',
						'title'=>'Delete'
				);
		$delete_button = button_icon( 'b_drop.png' , $deleteproperty  );
		 
		$row[] = array(
			'periode'	=> position_text_align ( date("m-Y", strtotime($ey['periode'] ." 00:00:00" ) ), 'left'), 
			'operasi'=> position_text_align(  $edit_button.' '.$detail_button.' '.$delete_button , 'right'),  
		);
	}
	
	$datas = table_rows($row);  
	return  table_builder($header , $datas ,  5 , false );
}

function form_grievance_periode_create($pid){
	$view = form_header( "calon karyawan" , "cp"  );
	if($pid>0){
		$fields = my_get_data_by_id('grievance_periode','grievance_periode_id',$pid);
		$month = date('m',strtotime($fields['periode']));
		$year = date('Y',strtotime($fields['periode']));	
	}else{
		$month = date('m');
		$year = date('Y');
	}
	$bulans  = array(
		'01'=>'01', 
		'02'=>'02', 
		'03'=>'03', 
		'04'=>'04', 
		'05'=>'05', 
		'06'=>'06', 
		'07'=>'07', 
		'08'=>'08', 
		'09'=>'09', 
		'10'=>'10', 
		'11'=>'11', 
		'12'=>'12', 
	);
	$bulan = array(
		'name'=>'bulan',
		'value'=>isset($_POST['bulan']) ? $_POST['bulan']: $month  
	);
	$bulan_form = form_dropdown( $bulan,$bulans);
	
	$tahun = array(
		'name'=>'tahun','size'=>5,
		'value'=>isset($_POST['tahun']) ? $_POST['tahun']: $year
	); 
	$tahun_form = form_dynamic($tahun );

	$view .= form_field_display( $bulan_form.' '. $tahun_form  , "Periode"    );
	
	
	$submit = array(
		'value' => (  ' Proses '  ),
		'name' => 'simpan', 
		'type'=>'submit','class'=>'main_button'
	);
	$form_submit= form_dynamic($submit);

	$view .= form_field_display( $form_submit, "&nbsp;" ,  "" );
	$view .= form_footer( );
	return $view;
}

function form_grievance_periode_submit($pid = 0){
	$periode = $_POST['tahun'].'-'.$_POST['bulan'].'-01';
	$datas = array(
			'periode'=> my_type_data_str($periode)	
	);
	if($pid > 0){
		return my_update_record( 'grievance_periode' , 'grievance_periode_id' , $pid , $datas );
	}
	return my_insert_record('grievance_periode' , $datas);
}

function form_grievance_periode_validate($pid,$id=0){
	$errsubmit = false;
	$err = array();
/*	 
	$nama =  trim($_POST["nama_grievance_kelompok"]); 
	if( $nama == ''){
		$errsubmit = true;
		$err[] = "Kelompok grievance belum diisi";
	}

	$query="SELECT grievance_kelompok_id FROM grievance_kelompok 
		WHERE nama_grievance_kelompok='".$nama.
		"' AND grievance_periode_id=".$pid. 
		" AND grievance_kelompok_id<>".$grievance_kelompok_id; 
	if (my_num_rows( my_query($query)) >0){
		$errsubmit = true;
		$err[] = "Kelompok grievance sudah ada";	
	}
 */   
	if( $errsubmit){
		return $err;
	}
	return $errsubmit;
}

function delete_periode_grievance($pid){
	$query = "DELETE FROM grievance_periode WHERE grievance_periode_id ={$pid}";
	return my_query($query);
}

/*  
==================================================================
2. KELOMPOK GRIEVANCE
==================================================================
*/

function list_grievance_kelompok($pid){
	my_set_code_js('
		function confirmDelete(id){
			var t = confirm(\'Yakin akan menghapus data ?\');
			if(t){
				location.href=\'index.php?com='.$_GET['com'].'&task=grievance_kelompok_delete&id=\'+id;
			}
			return false;
		}
	');

	$header = array(
		'#'=>array('style'=>'border-bottom:2px solid;width:5%'),  
		'Kelompok Grievance'=>array('style'=>'border-bottom:2px solid;width:85%'),  
		' '=>array('style'=>'border-bottom:2px solid;width:10%'), 
	);

	$query = "SELECT * FROM grievance_kelompok where grievance_periode_id=".$pid;
	 
	$result = my_query($query);
	$i = 0;
	$row=array();
	while($ey = my_fetch_array($result)){
		$i++;
		$editproperty = array(
			'href'=>'index.php?com=grievance&task=grievance_kelompok_create&pid='.$pid.'&id='.$ey['grievance_kelompok_id'],
			'rel'=>'facebox',
			'title'=>'Edit'
		);
		$edit_button = button_icon( 'b_edit.png' , $editproperty  );
		$detailproperty = array(
			'href'=>'index.php?com=grievance&task=list_grievance_sub&kid='.$ey['grievance_kelompok_id'],
			'title'=>'Sub Grievance'
		);
		$detail_button = button_icon( 'b_props.png' , $detailproperty  );

		$query="SELECT grievance_sub_id FROM grievance_sub 
			WHERE grievance_kelompok_id=".$ey['grievance_kelompok_id'] ;
		if (my_num_rows( my_query($query)) >0){
			$delete_button = button_icon( 'b_drop_off.png' , ''  );
		}else{
			$deleteproperty = array(
				'href'=>'javascript:;',
				'title'=>'Delete',
				'onclick'=>'confirmDelete('.$ey['grievance_kelompok_id'].')',
			);
			$delete_button = button_icon( 'b_drop.png' , $deleteproperty  );
		}	
 
		$row[] = array(
			'#'=>position_text_align ($i, 'center'), 
			'nama'=>  $ey['nama_grievance_kelompok'] ,   
			'operasi'=> position_text_align(  $edit_button.' '.$detail_button.' '.$delete_button, 'right'),  
		);
		 
	}
	$datas = table_rows($row);   
	return table_builder($header , $datas ,  3 , false );
} 

function form_grievance_kelompok_create( $id = 0){
	$view = form_header( "grievance_kelompok" , "grievance_kelompok"  );
 	$fields = my_get_data_by_id('grievance_kelompok','grievance_kelompok_id',$id);
 
	$nama = array(
			'name'=>'nama_grievance_kelompok',
			'value'=>( isset($_POST['nama_grievance_kelompok']) ? $_POST['nama_grievance_kelompok'] : $fields['nama_grievance_kelompok'] ),
			'id'=>'nama_grievance_kelompok',
			'type'=>'text',
			'size'=>'45'
			);
	$form_field_nama = form_dynamic($nama);
	$view .= form_field_display( $form_field_nama   , "Kelompok Grievance");
	
	$submit = array(
		'value' => ( $id ==0 ? ' Simpan ' :'  Update  '),
		'name' => 'simpan', 
		'type'=>'submit','class'=>'main_button'
	);
	$form_submit= form_dynamic($submit); 
	
	$view .= form_field_display( $form_submit  , "&nbsp;" ,  "" );
	$view .= form_footer( );
	return $view;
} 

	 
function form_grievance_kelompok_submit($pid,$id=0){
	$datas = array(
		'grievance_periode_id'=>my_type_data_int($pid),
		'nama_grievance_kelompok'=>my_type_data_str( $_POST['nama_grievance_kelompok'] ),
	);
	 
	if($id > 0){
		return my_update_record('grievance_kelompok' ,'grievance_kelompok_id' , $id , $datas);
	}
	
	return my_insert_record('grievance_kelompok' , $datas);
} 

function form_grievance_kelompok_validate($pid,$grievance_kelompok_id){
	$errsubmit = false;
	$err = array();
	 
	$nama =  trim($_POST["nama_grievance_kelompok"]); 
	if( $nama == ''){
		$errsubmit = true;
		$err[] = "Kelompok grievance belum diisi";
	}

	$query="SELECT grievance_kelompok_id FROM grievance_kelompok 
		WHERE nama_grievance_kelompok='".$nama.
		"' AND grievance_periode_id=".$pid. 
		" AND grievance_kelompok_id<>".$grievance_kelompok_id; 
	if (my_num_rows( my_query($query)) >0){
		$errsubmit = true;
		$err[] = "Kelompok grievance sudah ada";	
	}
    
	if( $errsubmit){
		return $err;
	}
	return $errsubmit;
}

function delete_grievance_kelompok($grievance_kelompok_id){
	$query = "DELETE FROM grievance_kelompok WHERE grievance_kelompok_id ={$grievance_kelompok_id} ";
	return my_query($query);
}	

/*  
==================================================================
2. Grievance Sub
==================================================================
*/

function list_grievance_sub($kid = 0){
	my_set_code_js('
		function confirmDelete(id){
			var t = confirm(\'Yakin akan menghapus data ?\');
			if(t){
				location.href=\'index.php?com='.$_GET['com'].'&task=grievance_sub_delete&id=\'+id;
			}
			return false;
		}
	');
	$kid = (int) $kid;
	if($kid == 0)my_direct($_SERVER['HTTP_REFERER']);
		$url = 'index.php?com=grievance&task=delete_sub_grievance&id=';
		remove_data_load_js( $url );
		$header = array(
			'#'=>array('style'=>'border-bottom:2px solid;width:5%'),  
			'Sub Grievance'=>array('style'=>'border-bottom:2px solid;width:85%'),  
			' '=>array('style'=>'border-bottom:2px solid;width:10%'), 
	);
 
	$query = "SELECT * FROM grievance_sub WHERE grievance_kelompok_id ={$kid}";
 	$result = my_query($query);
	$i = 0;
	$row=array();
	while($ey = my_fetch_array($result)){
		$i++;
		$editproperty = array(
			'href'=>'index.php?com=grievance&task=grievance_sub_create&kid='.$kid.'&id='.$ey['grievance_sub_id'],
			'rel'=>'facebox',
			'title'=>'grievance'
		);
		$edit_button = button_icon( 'b_edit.png' , $editproperty  );
		$detailproperty = array(
			'href'=>'index.php?com=grievance&task=list_grievance&sid='.$ey['grievance_sub_id'].'&kid='.$kid,
			'title'=>'grievance'
		);
		$detail_button = button_icon( 'b_props.png' , $detailproperty  );

		$query="SELECT grievance_id FROM grievance 
			WHERE grievance_sub_id=".$ey['grievance_sub_id'] ;
		if (my_num_rows( my_query($query)) >0){
			$delete_button = button_icon( 'b_drop_off.png' , ''  );
		}else{
			$deleteproperty = array(
				'href'=>'javascript:;',
				'title'=>'Delete',
				'onclick'=>'confirmDelete('.$ey['grievance_sub_id'].')',
			);
			$delete_button = button_icon( 'b_drop.png' , $deleteproperty  );
		}	
		
		$row[] = array(
			'#'=>position_text_align ($i, 'center'), 
			'nama'=>  $ey['nama_grievance_sub'] ,    
			'operasi'=> position_text_align(  $edit_button.' '.$detail_button.' '.$delete_button, 'right'),  
		);
		 
	}
	$datas = table_rows($row); 
	$grievance_kelompok = my_get_data_by_id('grievance_kelompok','grievance_kelompok_id',$kid); 	 
	$view ='<table width="90%" cellspacing="0" cellpadding="4"><tr>'; 
	$view .= form_header( "Unit grievance" , "Unit grievance"  );
	$view .= form_field_display( '<span class="label_form">'.ucfirst($grievance_kelompok['nama_grievance_kelompok']).'</span>', "<b>Kelompok grievance</b>"    ); 
	return  $view. table_builder($header , $datas ,  3 , false );
}

function form_grievance_sub_create($kid, $id = 0){
	$view = form_header( "grievance_sub" , "grievance_sub"  );
 	$fields = my_get_data_by_id('grievance_sub','grievance_sub_id',$id);
 	$kelompok = my_get_data_by_id('grievance_kelompok','grievance_kelompok_id',$kid); 
	$nama = array(
		'name'=>'nama_grievance_sub',
		'value'=>( isset($_POST['nama_grievance_sub']) ? $_POST['nama_grievance_sub'] : $fields['nama_grievance_sub'] ),
		'id'=>'nama_grievance_sub',
		'type'=>'text',
		'size'=>'45'
	);
	$form_field_nama = form_dynamic($nama);
	$view .= form_field_display( $form_field_nama   , "Sub Grievance");
	
	$submit = array(
		'value' => ( $id ==0 ? ' Simpan ' :'  Update  '),
		'name' => 'simpan', 
		'type'=>'submit','class'=>'main_button'
	);
	$form_submit= form_dynamic($submit); 
	$view .= form_field_display( $form_submit  , "&nbsp;" ,  "" );
	$view .= form_footer( );
	return $view;
} 
	 
function form_grievance_sub_submit($kid , $id =0){
	$datas = array(
		'grievance_kelompok_id'=>my_type_data_int($kid),	
		'nama_grievance_sub'=>my_type_data_str( $_POST['nama_grievance_sub'] ),
	);
	
	if($id > 0){
		return my_update_record('grievance_sub' ,'grievance_sub_id' , $id , $datas);
	}
	return my_insert_record('grievance_sub' , $datas);
} 

function form_grievance_sub_validate($grievance_sub_id){
	$errsubmit = false;
	$err = array();
	 
	$nama =  trim($_POST["nama_grievance_sub"]); 
	if( $nama == ''){
		$errsubmit = true;
		$err[] = "Sub grievance belum diisi";
	}

	$query="SELECT grievance_sub_id FROM grievance_sub 
		WHERE nama_grievance_sub='".$nama.
		"' AND grievance_sub_id<>".$grievance_sub_id; 
	if (my_num_rows( my_query($query)) >0){
		$errsubmit = true;
		$err[] = "Sub grievance sudah ada";	
	}
    
	if( $errsubmit){
		return $err;
	}
	return $errsubmit;
}

function delete_grievance_sub($grievance_sub_id){
	$query = "DELETE FROM grievance_sub WHERE grievance_sub_id ={$grievance_sub_id} ";
	return my_query($query);
}	

/*  
==================================================================
3. Grievance
==================================================================
*/

function list_grievance($sid = 0){
	my_set_code_js('
		function confirmDelete(id){
			var t = confirm(\'Yakin akan menghapus data ?\');
			if(t){
				location.href=\'index.php?com='.$_GET['com'].'&task=grievance_delete&id=\'+id;
			}
			return false;
		}
	');
	$sid = (int) $sid;
	if($sid == 0)my_direct($_SERVER['HTTP_REFERER']);
		$url = 'index.php?com=grievance&task=delete_sub_grievance&id=';
		remove_data_load_js( $url );
		$header = array(
			'#'=>array('style'=>'border-bottom:2px solid;width:5%'),  
			'Nama Grievance'=>array('style'=>'border-bottom:2px solid;width:85%'),  
			' '=>array('style'=>'border-bottom:2px solid;width:10%'), 
	);
 
	$query = "SELECT * FROM grievance WHERE grievance_sub_id ={$sid}";
 	$result = my_query($query);
	$i = 0;
	$row=array();
	while($ey = my_fetch_array($result)){
		$i++;
		$editproperty = array(
			'href'=>'index.php?com=grievance&task=grievance_create&sid='.$sid.'&id='.$ey['grievance_id'],
			'rel'=>'facebox',
			'title'=>'Edit'
		);
		$edit_button = button_icon( 'b_edit.png' , $editproperty  );

		$deleteproperty = array(
			'href'=>'javascript:;',
			'title'=>'Delete',
			'onclick'=>'confirmDelete('.$ey['grievance_id'].')',
		);
		$delete_button = button_icon( 'b_drop.png' , $deleteproperty  );
		
		$row[] = array(
			'#'=>position_text_align ($i, 'center'), 
			'nama'=>  $ey['nama_grievance'] ,    
			'operasi'=> position_text_align(  $edit_button.' '.$delete_button, 'right'),  
		);
		 
	}
	
	$datas = table_rows($row); 
	$grievance_sub = my_get_data_by_id('grievance_sub','grievance_sub_id',$sid); 	 
	$grievance_kelompok = my_get_data_by_id('grievance_kelompok','grievance_kelompok_id',$grievance_sub['grievance_kelompok_id']); 

	$view ='<table width="90%" cellspacing="0" cellpadding="4"><tr>'; 
	$view .= form_header( "Sub Grievance" , "Sub Grievance"  );
	$view .= form_field_display( '<span class="label_form">'.$grievance_kelompok['nama_grievance_kelompok'].'</span>', "<b>Kelompok Grievance</b>"); 
	$view .= form_field_display( '<span class="label_form">'.ucfirst($grievance_sub['nama_grievance_sub']).'</span>', "<b>Sub Grievance</b>"    ); 
	return  $view. table_builder($header , $datas ,  3 , false );
}

function form_grievance_create($sid, $id = 0){
	$view = form_header( "Grievance" , "Grievance"  );
 	$fields = my_get_data_by_id('grievance','grievance_id',$id);

	$c = array(
		'name'=>'nama_grievance',
		'value'=>( isset($_POST['nama_grievance']) ? $_POST['nama_grievance'] : $fields['nama_grievance'] ),
		'id'=>'nama_grievance',  
		'rows'=>'3',
		'size'=>'45'
	);
	$form_field_nama = form_textarea($c);
	$view .= form_field_display( $form_field_nama   , "Nama Grievance");

	$submit = array(
		'value' => ( $id ==0 ? ' Simpan ' :'  Update  '),
		'name' => 'simpan', 
		'type'=>'submit','class'=>'main_button'
	);
	$form_submit= form_dynamic($submit); 
	$view .= form_field_display( $form_submit  , "&nbsp;" ,  "" );
	$view .= form_footer( );
	return $view;
} 
	 
function form_grievance_submit($sid , $id =0){
	$datas = array(
		'grievance_sub_id'=>my_type_data_int($sid),	
		'nama_grievance'=>my_type_data_str( $_POST['nama_grievance'] ),
	);
	
	if($id > 0){
		return my_update_record('grievance' ,'grievance_id' , $id , $datas);
	}
	return my_insert_record('grievance' , $datas);
} 

function form_grievance_validate($grievance_id){
	$errsubmit = false;
	$err = array();

	$nama =  trim($_POST["nama_grievance"]); 
	if( $nama == ''){
		$errsubmit = true;
		$err[] = "Nama Grievance belum diisi";
	}

	$query="SELECT grievance_id FROM grievance 
		WHERE nama_grievance='".$nama.
		"' AND grievance_id<>".$grievance_id; 
	if (my_num_rows( my_query($query)) >0){
		$errsubmit = true;
		$err[] = "Nama Grievance sudah ada";	
	}
	
	if( $errsubmit){
		return $err;
	}
	return $errsubmit;
}

function delete_grievance($grievance_id){
	$query = "DELETE FROM grievance WHERE grievance_id ={$grievance_id} ";
	return my_query($query);
}	
